package org.dromara.module.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.helper.DataPermissionHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.handler.ICustomerInfoHandler;
import org.dromara.module.contact.service.IContactInfoService;
import org.dromara.module.customer.domain.bo.CustomerInfoBo;
import org.dromara.module.customer.domain.vo.CustomerInfoVo;
import org.dromara.module.customer.service.ICustomerInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公海客户信息
 *
 * @author weidixian
 * @date 2025-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/customer/info/public")
public class PublicCustomerInfoController extends BaseController {

    private final ICustomerInfoService customerInfoService;
    private final IContactInfoService contactInfoService;
    private final ICustomerInfoHandler customerInfoHandler;

    /**
     * 查询公海客户信息列表
     */
    @SaCheckPermission("customer:public:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerInfoVo> list(CustomerInfoBo bo, PageQuery pageQuery) {
        bo.getParams().put("isPublic", true); // 设置查询条件为公海客户
        TableDataInfo<CustomerInfoVo> tableDataInfo = DataPermissionHelper.ignore(() -> customerInfoService.queryPageList(bo, pageQuery));
        return tableDataInfo;
    }

    /**
     * 获取公海客户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("customer:public:query")
    @GetMapping("/{id}")
    public R<CustomerInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        CustomerInfoVo customerInfoVo = DataPermissionHelper.ignore(() -> customerInfoService.queryById(id));
        if (customerInfoVo == null && customerInfoVo.getAssignedTo() != null) { // 检查是否为公海客户
            return R.fail("数据不存在");
        }
        customerInfoVo.setContactInfo(DataPermissionHelper.ignore(() -> contactInfoService.queryById(customerInfoVo.getContactId())));
        return R.ok(customerInfoVo);
    }

    /**
     * 领取客户到指定用户
     */
    @SaCheckPermission("customer:public:claim")
    @PutMapping("/claim/{userId}/{customerIds}")
    public R<Void> claim(@NotNull(message = "用户ID不能为空") @PathVariable Long userId,
                         @NotNull(message = "客户ID不能为空") @PathVariable Long[] customerIds) {
        return toAjax(customerInfoHandler.claim(userId, List.of(customerIds)));
    }
}
